#include <Tlc5940.h>
#include <tlc_config.h>

//From the bildr article: http://bildr.org/2012/03/servos-tlc5940-arduino
//Requires the Tlc5940 library. http://code.google.com/p/tlc5940arduino/downloads/list

int RED = 0;
int GREEN = 1;
int BLUE = 2;
int MAXIMUM = 4095;

int window = 0;
int value = 0;
int color = RED;
int counter = 0;

void setup(){
  Tlc.init(4095);
  Serial.begin(9600);
  for (window = 0; window < 5; window++) {
    setWindowColor(window, 1024, 0, 0);
  }
}

void loop() {
  for (window = 0; window < 5; window++) {
    setWindowColor(window, 0, 0, 0);
  }
  setWindowColor(0, 3095, 0, 0);
  update();
}

void setWindowColor(int window, int red, int green, int blue) {
  int redPin = getRedPin(window);
  int greenPin = getGreenPin(window);
  int bluePin = getBluePin(window);
  
  setColor(redPin, red);
  setColor(greenPin, green);
  setColor(bluePin, blue);
}

int getRedPin(int window) {
  int firstPin = window * 3;
  return firstPin;
}

int getGreenPin(int window) {
  int firstPin = window * 3;
  return firstPin + 1;
}

int getBluePin(int window) {
  int firstPin = window * 3;
  return firstPin + 2;
}

void update() {
  Tlc.update();
  delay(1000);
//  delayMicroseconds(100);
}
void setAllWindowColor(int red, int green, int blue) {
  int window = 0;
  for (window = 0; window < 5; window++) {
    setWindowColor(window, red, green, blue);
  }
}

void setAllColor(int color) {
  int i;
  for (i = 0; i < 16; i++) {
    setColor(i, color);
  }
}

void setColor(int pin, int color) {
  Tlc.set(pin, MAXIMUM - color);
}

